package root.electronclass.electronclass.home.widget

import android.content.Intent
import androidx.activity.viewModels
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.CommonDialogFragment
import root.electronclass.electronclass.common.base.ViewBindingActivity
import root.electronclass.electronclass.R
import root.electronclass.electronclass.databinding.ActivityMessageBinding
import root.electronclass.electronclass.databinding.ActivityMessageItemBinding
import root.electronclass.electronclass.ext.CoroutineExceptionHandler
import root.electronclass.electronclass.ext.doOnSearch
import root.electronclass.electronclass.ext.tabs
import root.electronclass.electronclass.personal.dialog.MessageDialog
import root.electronclass.electronclass.setup
import root.electronclass.electronclass.mvp.entity.Message
import root.electronclass.electronclass.mvp.rest.RestManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MessageActivity : ViewBindingActivity<ActivityMessageBinding>() {
    private val viewModel: MessageModel by viewModels()
    private var dataType = false
    override fun init() {
        val sendUserType = intent.getIntExtra("sendUserType", 1)
        binding.top.setup(lifecycle, "我的消息")
        binding.back.back.setOnClickListener {
            setResult(RESULT_OK,Intent())
            finish()
        }

        val tabs = resources.getStringArray(R.array.message)
        binding.tabLayout.tabs(tabs) { tab ->
            val text = binding.search.text.toString()
            when (tab) {
                "未读消息" -> dataType = false
                "已读消息" -> dataType = true

            }
            viewModel.load(type = dataType, text = text, sendUserType)
        }

        binding.recycler.adapter = adapter
        binding.search.doOnSearch {
            viewModel.load(text = it, sendUserType = sendUserType, page = 1)
            false
        }

        viewModel.data.observe(this) {

            adapter.notifyChanged(it.second)

        }
        viewModel.load(type = false, text = "", sendUserType)
    }

    @Suppress("UNCHECKED_CAST")
    private val adapter = BasicAdapter<Message, ActivityMessageItemBinding>({ viewModel.load() }) { holder, item ->
        val lastItem = list.getOrNull(holder.adapterPosition - 1)
        val yyyyMMdd = item.pushTime.take(10)
        val showHeader = lastItem?.pushTime?.take(10) != yyyyMMdd
        holder.binding.itemTitle.isVisible = showHeader
        holder.binding.itemDot.isVisible = showHeader
        if (showHeader) {
            holder.binding.itemTitle.text = yyyyMMdd
        }
        holder.binding.itemTitle.text = item.pushTime
        holder.binding.include.ivMessgae.setImageResource(if (item.isRead) R.drawable.messgae_read else R.drawable.message_unread)
        holder.binding.include.itemTitle.text = item.mesageTitle
        holder.binding.include.itemDesc.text = item.pushTime

        holder.binding.include.notificationItem.setOnClickListener {
            lifecycleScope.launch(CoroutineExceptionHandler()) {
                withContext(Dispatchers.IO) {
                    RestManager.getInstance().initRest().messageDetails(item.id)
                }.rows?.let { message ->
                    MessageDialog().also {
                        it.arguments = bundleOf(
                            CommonDialogFragment.TITLE to "消息详情",
                            MessageDialog.DATA to message,
                            CommonDialogFragment.CLOSE to true
                        )
                    }.show(supportFragmentManager, "MESS")
                }

            }
        }

    }

}